package com.qfedu.bigdataapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qfedu.bigdataapp.R;
import com.qfedu.bigdataapp.bean.NewsTopic;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 新闻头条的适配器类
 */
public class NewsTopicAdapter extends BaseAdapter {
    private Context context;
    private List<NewsTopic> newsTopicList;

    /**
     * 构造方法
     *
     * @param context   上下文对象
     * @param topicList 新闻头条的数据
     */
    public NewsTopicAdapter(Context context, List<NewsTopic> topicList) {
        this.context = context;
        this.newsTopicList = topicList;
    }

    /**
     * 追加新数据
     *
     * @param topicList 要追加的新数据
     */
    public void addNewData(List<NewsTopic> topicList) {
        newsTopicList.addAll(topicList);
    }


    @Override
    public int getCount() {
        return newsTopicList.size();
    }

    @Override
    public NewsTopic getItem(int i) {
        return newsTopicList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //1、确定要展示的数据
        NewsTopic topic = newsTopicList.get(i);

        //2、确定要展示数据的布局模板
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_newstopic, viewGroup, false);
        }

        TextView title = view.findViewById(R.id.tv_newstopic_title);
        ImageView imgCover1 = view.findViewById(R.id.img_cover);
        ImageView imgCover02 = view.findViewById(R.id.img_cover02);
        ImageView imgCover03 = view.findViewById(R.id.img_cover03);

        //3、为控件设置相应的数据
        title.setText(topic.getTitle());
        //设置新闻封面图片
        if (!"".equals(topic.getThumbnail_pic_s())) {
            Picasso.with(context).load(topic.getThumbnail_pic_s()).into(imgCover1);
        }
        if (!"".equals(topic.getThumbnail_pic_s02())) {
            Picasso.with(context).load(topic.getThumbnail_pic_s02()).into(imgCover02);
        }
        if (!"".equals(topic.getThumbnail_pic_s03())) {
            Picasso.with(context).load(topic.getThumbnail_pic_s03()).into(imgCover03);
        }

        return view;
    }
}
